﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace BenefitHelper.Tools
{
    public class BaseControl : Controller
    {


        /// <summary>
        /// 得到分析系统当前所有的组
        /// </summary>
        public List<BenefitHelper.Sys.Groups> groups = null;
        /// <summary>
        /// 得到分析系统当前所有的操作员
        /// </summary>
        public List<BenefitHelper.Sys.Operator> operators = null;
        /// <summary>
        /// 得到当前有效的小组分组情况(以最后一个交易日为准)
        /// </summary>
        public List<BenefitHelper.Sys.DayGroupOperators> dayGroupOperators = null;
        /// <summary>
        /// 当前登录用户(操作员)
        /// </summary>
        public BenefitHelper.Sys.Operator op = null;
        /// <summary>
        /// 当前登录用户当月收益情况
        /// </summary>
        public BenefitHelper.View.OperatorMonthSum loginOperatorMonthSum = null;
        /// <summary>
        /// 当前登录用户上月收益情况
        /// </summary>
        public BenefitHelper.View.OperatorMonthSum loginOperatorLastMonthSum = null;
        /// <summary>
        /// 当前登录用户当月预期值
        /// </summary>
        public double loginOperatorMonthExpected = 0;

        public DB.DBManager db = null;

        public BaseControl()
        {
            db = new DB.DBManager();
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            int nowdayId = Convert.ToInt32(cookie.GetCookie("TradingDayId"));

            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            ViewBag.loginuserid = operId;
            string TradingDayPdate = cookie.GetCookie("TradingDayPdate");
            if (nowdayId != 0 && operId != 0 && !string.IsNullOrEmpty(TradingDayPdate))
            {
                if (Sys.ServerStatus.GetServerStatus() == Convert.ToInt32(Sys._ServerStatus.Free))
                {
                    #region 每次都需要先判断查看权限
                    Config.GlobalPrower prower = new Config.GlobalPrower();

                    bool hasPrower = prower.AllowLogin(db);
                    string loginUserType = cookie.GetCookie("LoginUserType");
                    #endregion

                    if (hasPrower && !string.IsNullOrEmpty(loginUserType))
                    {
                        if (loginUserType.Equals(Convert.ToInt32(Config.UserType.Operator).ToString()))//操作员
                        {
                            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();

                            th = th.GetModel(nowdayId, db);
                            //   nowdayId.ToString().Debug();
                            groups = new BenefitHelper.Sys.OperatorCouldSeeGroups().GetOperatorCouldSeeGroups(operId, db);

                            ViewBag.groups = groups;

                            operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                            ViewBag.operators = operators;

                            dayGroupOperators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(th.GetDateTimeFromTradeHistoryId(nowdayId, db), db);
                            ViewBag.dayoperatorgroup = dayGroupOperators;


                            ViewBag.TradingDayPdate = TradingDayPdate;

                            BenefitHelper.Sys.OperatorInfo info = new BenefitHelper.Sys.OperatorInfo();
                            info = info.GetOperatorInfo(operId, db);
                            ViewBag.operatorinfo = info;

                            op = new Sys.Operator();
                            op = op.GetModel(operId, db);
                            ViewBag.oper = op;

                            BenefitHelper.Sys.DayGroupOperators dgop = dayGroupOperators.Where(a => a.OperatorId == op.Id).Where(a => a.Pdate.Year == th.YPdate).Where(a => a.Pdate.Month == th.MPdate).Where(a => a.Pdate.Day == th.DPdate).First();

                            //  var query = groups.Where(a => a.Id == dgop.GroupId);
                            Sys.Groups mygroup = db.Groups.Where(a => a.Id == dgop.GroupId).First();

                            ViewBag.groupname = mygroup.GroupName;

                            int mysort = new Data.Week.OperatorWeekSort().GetOperatorSort(nowdayId, operId, db);
                            ViewBag.mysort = mysort;

                            loginOperatorMonthSum = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSum(operId, th.MPdate, th.YPdate, db);
                            ViewBag.loginOperatorMonthSum = loginOperatorMonthSum;//本月累计收益

                            DateTime pdate = th.GetLastMonth(th.MPdate, th.YPdate);
                            loginOperatorLastMonthSum = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSum(operId, pdate.Month, pdate.Year, db);
                            if (loginOperatorLastMonthSum == null)
                            {
                                loginOperatorLastMonthSum = new View.OperatorMonthSum();
                            }
                            ViewBag.loginOperatorLastMonthSum = loginOperatorLastMonthSum;//上月收益

                            loginOperatorMonthExpected = new BenefitHelper.View.OperatorMonthExpected().GetOperatorMonthExpected(operId, th.YPdate, th.MPdate, db).DayCount;//本月预期

                            ViewBag.loginOperatorMonthExpected = loginOperatorMonthExpected;


                            int growthIndex = op.GetGrowthIndex(loginOperatorLastMonthSum.Profit, loginOperatorMonthSum.Profit);
                            int stableIndex = op.GetStableIndex(loginOperatorLastMonthSum.Profit, loginOperatorMonthSum.Profit);
                            ViewBag.growthIndex = growthIndex;
                            ViewBag.stableIndex = stableIndex;

                            int wdzsCount = op.GetPicCount(stableIndex);//稳定指数

                            ViewBag.wdzsCount = wdzsCount;
                            int czzsCount = op.GetPicCount(growthIndex);//成长指数

                            ViewBag.czzsCount = czzsCount;

                            ViewBag.lengths = GetMonthSumAllLength(loginOperatorMonthSum.Profit, loginOperatorLastMonthSum.Profit, 400);

                        }
                        else//管理员
                        {
                            ViewBag.usertype = "2";
                            cookie.SetCookie("LoginUserType", "2", 48);
                            groups = new BenefitHelper.Sys.OperatorCouldSeeGroups().GetOperatorCouldSeeGroups(operId, db);

                            ViewBag.groups = groups;

                            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();

                            th = th.GetModel(nowdayId, db);
                            dayGroupOperators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(th.GetDateTimeFromTradeHistoryId(nowdayId, db), db);
                            ViewBag.dayoperatorgroup = dayGroupOperators;

                            operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                            ViewBag.operators = operators;
                            ViewBag.TradingDayPdate = TradingDayPdate;
                        }
                    }
                    else
                    {
                        System.Web.HttpContext.Current.Response.Redirect("/");
                    }


                }
                else
                {
                    System.Web.HttpContext.Current.Response.Redirect("/Home/Busy");
                }
            }
            else
            {
                System.Web.HttpContext.Current.Response.Redirect("/Home/Index");
            }
        }
        /// <summary>
        /// 得到收益条的长度
        /// </summary>
        /// <param name="monthSum">本月当前盈利</param>
        /// <param name="lastMonthSum">上月盈利</param>
        /// <param name="expectedSum">本月预期盈利</param>
        /// <returns>数组，本月盈利条长度，上月盈利条长度，本月预期条长度</returns>
        private int[] GetMonthSumAllLength(double monthSum, double lastMonthSum, double expectedSum)
        {
            int[] lengths = new int[3];
            int maxLength = 80;
            if (monthSum == 0)
            {
                monthSum = 1;
            }
            if (expectedSum == 0)
            {
                expectedSum = 1;
            }
            if (lastMonthSum == 0)
            {
                lastMonthSum = 1;
            }
            lengths[0] = Convert.ToInt32((monthSum * maxLength) / expectedSum);
            lengths[1] = Convert.ToInt32((lastMonthSum * maxLength) / expectedSum);
            lengths[2] = maxLength;

            for (int i = 0; i < lengths.Length; i++)
            {
                if (lengths[i] <= 0)
                {
                    lengths[i] = 5;
                }
            }
            return lengths;
        }
    }
}
